package org.signal.glide;

import android.content.Context;
import android.content.res.AssetManager;
import android.net.Uri;
import android.util.Log;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.data.DataFetcher;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class CustomImageDataFetcher implements DataFetcher<InputStream> {
    private static final String TAG = "CustomImageDataFetcher";
    private final Context context;
    private final CustomImageModel model;
    private InputStream stream;

    public CustomImageDataFetcher(Context context, CustomImageModel model) {
        this.context = context;
        this.model = model;
    }

    @Override
    public void loadData(Priority priority, DataCallback<? super InputStream> callback) {
        try {
            Log.d(TAG, "Loading image: " + model.getImageId() + ", type: " + model.getImageType());
            
            switch (model.getImageType()) {
                case LOCAL:
                    // 从应用内部存储加载
                    File file = new File(context.getFilesDir(), model.getImageId());
                    Log.d(TAG, "Local file path: " + file.getAbsolutePath());
                    if (!file.exists()) {
                        throw new IOException("Local file not found: " + file.getAbsolutePath());
                    }
                    stream = context.getContentResolver().openInputStream(Uri.fromFile(file));
                    break;
                    
                case REMOTE:
                    // 实现远程图片加载逻辑
                    URL url = new URL(model.getImageId());
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setConnectTimeout(5000);
                    connection.setReadTimeout(5000);
                    connection.connect();
                    
                    int responseCode = connection.getResponseCode();
                    if (responseCode != HttpURLConnection.HTTP_OK) {
                        throw new IOException("HTTP error code: " + responseCode);
                    }
                    
                    stream = connection.getInputStream();
                    break;
                    
                case ASSET:
                    // 从 assets 目录加载
                    AssetManager assetManager = context.getAssets();
                    String assetPath = "images/" + model.getImageId(); // 假设图片在 assets/images/ 目录下
                    Log.d(TAG, "Asset path: " + assetPath);
                    stream = assetManager.open(assetPath);
                    break;
            }
            
            if (stream == null) {
                throw new IOException("Failed to open stream for: " + model.getImageId());
            }
            
            callback.onDataReady(stream);
        } catch (IOException e) {
            Log.e(TAG, "Failed to load image: " + model.getImageId(), e);
            callback.onLoadFailed(e);
        }
    }

    @Override
    public void cleanup() {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
                Log.e(TAG, "Failed to close stream", e);
            }
        }
    }

    @Override
    public void cancel() {
        // Do nothing.
    }

    @Override
    public Class<InputStream> getDataClass() {
        return InputStream.class;
    }

    @Override
    public DataSource getDataSource() {
        switch (model.getImageType()) {
            case LOCAL:
                return DataSource.LOCAL;
            case REMOTE:
                return DataSource.REMOTE;
            case ASSET:
                return DataSource.DATA_DISK_CACHE;  // 或者 DataSource.LOCAL
            default:
                throw new IllegalStateException("Unknown image type: " + model.getImageType());
        }
    }
} 